package com.qys.livesMall.finance.service.impl;

import com.qys.livesMall.common.exception.enums.ExceptionEnum;
import com.qys.livesMall.finance.enums.AccountBusinessTypeEnum;
import com.qys.livesMall.finance.enums.AccountTypeEnum;

import com.qys.livesMall.finance.model.dto.AccountInfo;
import com.qys.livesMall.finance.model.dto.TransferAccountHandle;
import com.qys.livesMall.finance.model.dto.shop.ShopRWHandle;
import com.qys.livesMall.finance.model.dto.user.UserRWHandle;
import com.qys.livesMall.finance.service.FinanceShopBusinessService;
import com.qys.livesMall.finance.service.TransferAccountsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @menu FinanceBusinessServiceImpl
 * @date 2023/07/29 17:37
 */
@Service
public class FinanceShopBusinessServiceImpl implements FinanceShopBusinessService {

    @Autowired
    private TransferAccountsService transferAccountsService;


    @Override
    public void shopRecharge(ShopRWHandle handle) {
        handle.setSuccess(false);
        if (handle.getUserId()==null||handle.getAmountIncurred()==null){
            handle.setMessage(ExceptionEnum.PARAM_IS_ERROR);
        }
        //构建转账对象
        AccountInfo out=AccountInfo.build(AccountTypeEnum.R_W_ACCOUNT);
        AccountInfo in=AccountInfo.build(AccountTypeEnum.SHOP_ACCOUNT, handle.getUserId(),handle.getShopId());
        TransferAccountHandle transfer=TransferAccountHandle.build(out,in,handle);
        transfer.setBusinessType(AccountBusinessTypeEnum.SHOP_RECHARGE);
        //转账
        transferAccountsService.transfer(transfer);

        handle.setSerialNumber(handle.getSerialNumber());
        handle.setSuccess(transfer.isSuccess());
        handle.setMessage(transfer.getMessage());
    }

    @Override
    public void shopWithdrawalsApplyFor(ShopRWHandle handle) {
        handle.setSuccess(false);
        if (handle.getUserId()==null||handle.getAmountIncurred()==null){
            handle.setMessage(ExceptionEnum.PARAM_IS_ERROR);
        }
        //构建转账对象
        AccountInfo out=AccountInfo.build(AccountTypeEnum.SHOP_ACCOUNT,handle.getUserId(), handle.getShopId());
        AccountInfo in=AccountInfo.build(AccountTypeEnum.ALLOCATION_ACCOUNT, handle.getUserId(), handle.getShopId());
        TransferAccountHandle transfer=TransferAccountHandle.build(out,in,handle);
        transfer.setBusinessType(AccountBusinessTypeEnum.SHOP_WITHDRAWAL);
        //转账
        transferAccountsService.transfer(transfer);

        handle.setSerialNumber(handle.getSerialNumber());
        handle.setSuccess(transfer.isSuccess());
        handle.setMessage(transfer.getMessage());
    }

    @Override
    public void shopWithdrawals(ShopRWHandle handle) {
        handle.setSuccess(false);
        if (handle.getUserId()==null||handle.getAmountIncurred()==null){
            handle.setMessage(ExceptionEnum.PARAM_IS_ERROR);
        }
        //构建转账对象
        AccountInfo out=AccountInfo.build(AccountTypeEnum.ALLOCATION_ACCOUNT,handle.getUserId(), handle.getShopId());
        AccountInfo in=AccountInfo.build(AccountTypeEnum.R_W_ACCOUNT, handle.getUserId(), handle.getShopId());
        TransferAccountHandle transfer=TransferAccountHandle.build(out,in,handle);
        transfer.setBusinessType(AccountBusinessTypeEnum.SHOP_WITHDRAWAL);
        //转账
        transferAccountsService.transfer(transfer);

        handle.setSerialNumber(handle.getSerialNumber());
        handle.setSuccess(transfer.isSuccess());
        handle.setMessage(transfer.getMessage());
    }

    @Override
    public void cancelShopWithdrawals(ShopRWHandle handle) {
        handle.setSuccess(false);
        if (handle.getUserId()==null||handle.getAmountIncurred()==null){
            handle.setMessage(ExceptionEnum.PARAM_IS_ERROR);
        }
        //构建转账对象
        AccountInfo out=AccountInfo.build(AccountTypeEnum.ALLOCATION_ACCOUNT,handle.getUserId(), handle.getShopId());
        AccountInfo in=AccountInfo.build(AccountTypeEnum.SHOP_ACCOUNT, handle.getUserId(), handle.getShopId());
        TransferAccountHandle transfer=TransferAccountHandle.build(out,in,handle);
        transfer.setBusinessType(AccountBusinessTypeEnum.SHOP_WITHDRAWAL);
        //转账
        transferAccountsService.transfer(transfer);

        handle.setSerialNumber(handle.getSerialNumber());
        handle.setSuccess(transfer.isSuccess());
        handle.setMessage(transfer.getMessage());
    }

}

